package com.workerhelping.config;

import com.workerhelping.constant.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类
 *  - 交换机
 *  - 队列
 */
@Configuration
public class RabbitMQConfig {


    // 档案
    @Bean("exchange_record")
    public Exchange exchange_record() {
        return ExchangeBuilder.directExchange(MQConstant.EXCHANGE_RECORD).build();
    }

    @Bean("queue_record_delete")
    public Queue queue_record_delete() {
        return QueueBuilder.durable(MQConstant.QUEUE_RECORD_DELETE).build();
    }

    @Bean("queue_record_insertOrUpdate")
    public Queue queue_record_insertOrUpdate() {
        return QueueBuilder.durable(MQConstant.QUEUE_RECORD_INSERTORUPDATE).build();
    }

    @Bean
    public Binding QRDBindingER(@Qualifier("queue_record_delete") Queue queue_record_delete,
                                @Qualifier("exchange_record") Exchange exchange_record) {
        return BindingBuilder.bind(queue_record_delete).to(exchange_record).with(MQConstant.ROUTINGKEY_RECORD_DELETE).noargs();
    }

    @Bean
    public Binding QRIOUBindingER(@Qualifier("queue_record_insertOrUpdate") Queue queue_record_insertOrUpdate,
                                  @Qualifier("exchange_record") Exchange exchange_record) {
        return BindingBuilder.bind(queue_record_insertOrUpdate).to(exchange_record).with(MQConstant.ROUTINGKEY_RECORD_INSERTORUPDATE).noargs();
    }

    // 收入
    @Bean("exchange_income")
    public Exchange exchange_income() {
        return ExchangeBuilder.directExchange(MQConstant.EXCHANGE_INCOME).build();
    }

    @Bean("queue_income_insert")
    public Queue queue_income_insert() {
        return QueueBuilder.durable(MQConstant.QUEUE_INCOME_INSERT).build();
    }

    @Bean
    public Binding QIIBindingEI(@Qualifier("queue_income_insert") Queue queue_income_insert,
                                @Qualifier("exchange_income") Exchange exchange_income) {
        return BindingBuilder.bind(queue_income_insert).to(exchange_income).with(MQConstant.ROUTINGKEY_INCOME_INSERT).noargs();
    }

    // 补助
    @Bean("exchange_assist")
    public Exchange exchange_assist() {
        return ExchangeBuilder.directExchange(MQConstant.EXCHANGE_ASSIST).build();
    }

    @Bean("queue_assist_insert")
    public Queue queue_assist_insert() {
        return QueueBuilder.durable(MQConstant.QUEUE_ASSIST_INSERT).build();
    }

    @Bean
    public Binding QAIBindingEA(@Qualifier("queue_assist_insert") Queue queue_assist_insert,
                                @Qualifier("exchange_assist") Exchange exchange_assist) {
        return BindingBuilder.bind(queue_assist_insert).to(exchange_assist).with(MQConstant.ROUTINGKEY_ASSIST_INSERT).noargs();
    }

    /**
     * 消息转换器
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
